package com.marcosnobrega.android.activity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MessagesList extends ListActivity{
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.messageslist);
		
		HttpGet get = new HttpGet("http://10.0.2.2/android.php");
		DefaultHttpClient client = new DefaultHttpClient();
		try {
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			String responseText = EntityUtils.toString(entity);
			String[] jsonStrings = responseText.split(";;;");
			adapter.clear();
			for(String s : jsonStrings){
				String[] mensagem = s.split(",");
				adapter.add(mensagem[0]+" - "+mensagem[1]);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			adapter.add(e.getMessage());
			e.printStackTrace();
		}
		
		setListAdapter(adapter);
	}

}
